package com.lzxzjy.serivce.impl;


import com.lzxzjy.mapper.MessageMapper;
import com.lzxzjy.pojo.Message;
import com.lzxzjy.pojo.PageBean;

import com.lzxzjy.serivce.MessageService;
import com.lzxzjy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class MessageServiceImpl implements MessageService {
    private final SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public boolean insertMessage(Message message) {
        SqlSession messageSqlSession = factory.openSession();
        MessageMapper messageMapper = messageSqlSession.getMapper(MessageMapper.class);
        Integer integer = messageMapper.insertMessage(message);
        messageSqlSession.commit();
        messageSqlSession.close();
        return integer != 0;
    }

    @Override
    public PageBean<Message> selectMessageByPageAndTerms(Message message, int currentPage, int pageSize) {
        SqlSession messageSqlSession = factory.openSession();
        MessageMapper messageMapper = messageSqlSession.getMapper(MessageMapper.class);
        int begin = (currentPage - 1) * pageSize;
        List<Message> messages = messageMapper.selectMessageByPageAndTerms(message, begin, pageSize);
        Integer messageTotalCountByTerms = messageMapper.selectMessageTotalCountByTerms(message);
        PageBean<Message> messagePageBean = new PageBean<>();
        messagePageBean.setRows(messages);
        messagePageBean.setTotalCount(messageTotalCountByTerms);
        messageSqlSession.close();
        return messagePageBean;
    }

    @Override
    public PageBean<Message> selectMessageByPageAndTerms(Message message) {
        SqlSession messageSqlSession = factory.openSession();
        MessageMapper messageMapper = messageSqlSession.getMapper(MessageMapper.class);
        List<Message> messages = messageMapper.selectMessageByPageAndTerms(message, 0, messageMapper.selectMessageTotalCountByTerms(message));
        Integer messageTotalCountByTerms = messageMapper.selectMessageTotalCountByTerms(message);
        PageBean<Message> messagePageBean = new PageBean<>();
        messagePageBean.setRows(messages);
        messagePageBean.setTotalCount(messageTotalCountByTerms);
        messageSqlSession.close();
        return messagePageBean;
    }

    @Override
    public boolean deleteMessageByIds(int[] messageIds) {
        SqlSession messageSqlSession = factory.openSession();
        MessageMapper messageMapper = messageSqlSession.getMapper(MessageMapper.class);
        Integer integer = messageMapper.deleteByMessageIds(messageIds);
        messageSqlSession.commit();
        messageSqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean deleteMessageById(int messageId) {
        SqlSession messageSqlSession = factory.openSession();
        MessageMapper messageMapper = messageSqlSession.getMapper(MessageMapper.class);
        Integer integer = messageMapper.deleteByMessageId(messageId);
        messageSqlSession.commit();
        messageSqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean updateMessageById(Message message) {
        SqlSession messageSqlSession = factory.openSession();
        MessageMapper messageMapper = messageSqlSession.getMapper(MessageMapper.class);
        Integer integer = messageMapper.updateByMessageId(message);
        messageSqlSession.commit();
        messageSqlSession.close();
        return integer != 0;
    }
}
